package com.qwc.blog.web.admin;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qwc.blog.common.annotation.AdminController;
import com.qwc.blog.common.constant.CommonConstant;
import com.qwc.blog.entity.Article;
import com.qwc.blog.entity.BlogLog;
import com.qwc.blog.entity.dto.ArticleDto;
import com.qwc.blog.entity.dto.ArticleRequestDto;
import com.qwc.blog.service.IArticleService;
import com.qwc.blog.service.IBlogLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 文章元信息表 前端控制器
 * </p>
 *
 * @author qwc
 * @since 2022-02-16
 */
@Slf4j
@AdminController("/article")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    @Autowired
    private IBlogLogService blogLogService;

    @GetMapping("/page")
    public ResponseEntity<Object> page(ArticleRequestDto<Article> dto) {
        return ResponseEntity.ok(articleService.pageList(dto));
    }

    @PostMapping
    public ResponseEntity<Object> add(@RequestBody @Validated ArticleDto dto) {
        return ResponseEntity.ok(articleService.saveArticleAndContent(dto));
    }

    @GetMapping("/{id}")
    public ResponseEntity<Object> get(@PathVariable Integer id) {
        return ResponseEntity.ok(articleService.getByArticleId(id));
    }

    @PutMapping
    public ResponseEntity<Object> update(@RequestBody @Validated ArticleDto dto) {
        return ResponseEntity.ok(articleService.updateByArticleId(dto));
    }

    @DeleteMapping("{ids}")
    public ResponseEntity<Object> deleteByIds(@PathVariable @NotNull(message = "请选择删除数据") Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        List<Article> articles = articleService.listByIds(list);
        articles.forEach(article -> {
            if (CommonConstant.ABLE.equals(article.getStatus())) {
                BlogLog blogLog = new BlogLog();
                blogLog.setBlogEvent(CommonConstant.DISABLE);
                blogLog.setBlogEventDesc(article.getTitle());
                blogLogService.save(blogLog);
            }
        });
        return ResponseEntity.ok(articleService.removeAllByIds(list));
    }

    @PutMapping("/{ids}/{status}")
    public ResponseEntity<Object> release(@PathVariable @NotNull(message = "请选择数据") Integer[] ids,
                                          @PathVariable @NotBlank(message = "状态不能为空") String status) {
        List<Integer> list = Arrays.asList(ids);
        List<Article> articles = articleService.listByIds(list);
        articles.forEach(article -> {
            BlogLog blogLog = new BlogLog();
            blogLog.setBlogEvent(status);
            blogLog.setBlogEventDesc(article.getTitle());
            blogLogService.save(blogLog);
        });
        return ResponseEntity.ok(articleService.update(null, Wrappers.<Article>lambdaUpdate()
                .set(Article::getStatus, status)
                .in(Article::getArticleId, ids)));
    }

    @PutMapping("/comment/{id}/{comment}")
    public ResponseEntity<Object> updateComment(@PathVariable @NotNull(message = "请选择数据") Integer id,
                                                @PathVariable @NotBlank(message = "评论状态不能为空") String comment) {
        return ResponseEntity.ok(articleService.update(null, Wrappers.<Article>lambdaUpdate()
                .set(Article::getIsComment, comment)
                .eq(Article::getArticleId, id)));
    }
}
